<%@ page language="java" pageEncoding="UTF-8" isELIgnored="false"%>
<%@page import="global.Constants"%>
<html>
<head>
	<%
		String baseUrl = request.getContextPath();
	%>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
	    //这里的全局是为了修改之后的刷新
	    var roleListGrid;
		$(document).ready(function(){
			// 全局参数
			var baseParams = {limit:<%=Constants.PAGE_SIZE %>,start : 0};
			var EXT_QUERY_TIME_OUT = 1200000;

			// 用户角色数据源
		    var roleListStore = new Ext.data.JsonStore({
		        url: '${pageContext.request.contextPath}/returnRoleInforPagesize.action',
		        root: 'datas',
				timeout : EXT_QUERY_TIME_OUT,
		        totalProperty: 'results',
		        fields: ['role_id', 'role_name', 'role_describe'],
		        baseParams: baseParams,
		        autoLoad: true,
				listeners : {  
          load : function() {  
              isDetailReady = true;// 查询置换标志为已查过   
            }  
            }  
		    });
			
		   	    // 工具栏
		    var roleListToolbar = new Ext.Toolbar({
		    	renderTo: 'roleListToolBarDiv',
		    	items: [
					new Ext.Button({
					    id: 'roleList-add-button',
					    text: '添加',
						iconCls: 'user_add'
						//disabled:true
					}),
					
					new Ext.Button({
					    id: 'roleList-update-button',
					    text: '编辑角色',
						iconCls: 'user_edit'
					}),
					new Ext.Button({
					    id: 'roleList-delete-button',
					    text: '删除',
						iconCls: 'user_delete'
					}),
					'-',
					new Ext.Button({
					    id: 'roleList-setting-button',
					    text: '权限设置',
						iconCls: 'user_go'
					})
				]
			});

			// 用户角色数据表格
			var sm = new Ext.grid.CheckboxSelectionModel();
			roleListGrid = new Ext.grid.GridPanel({
				id: 'roleListGrid',
				renderTo: 'roleListGridDiv',
				border: false,
				stateful: true,
			    autoScroll: 'auto',
		        store: roleListStore,
		        loadMask: true,
		        cm: new Ext.grid.ColumnModel({
		            defaults: {
		                width: 120,
		                sortable: true
		            },
			        columns: [
						sm,
						new Ext.grid.RowNumberer({header:'№',width:50}),
						{id:'role_id',header: '角色ID', width: 100, hidden: true, sortable: true, dataIndex: 'role_id'},
			            {id:'role_name',header: '角色名', width: 100, sortable: true, dataIndex: 'role_name'},
			            {id:'role_describe',header: '角色描述', width: 280, sortable: true, dataIndex: 'role_describe'}
					]
		        }),
		        sm: sm,
		        columnLines: true,
		        bbar: new Ext.PagingToolbar({
					pageSize: <%=Constants.PAGE_SIZE %>,
					store: roleListStore,
					displayInfo: true,
					displayMsg: Anynote.PAGINGTOOLBAR_DISPLAYMSG,
					emptyMsg: Anynote.PAGINGTOOLBAR_EMPTYMSG,
					doLoad: function(start){
						baseParams.start = start;
						this.store.load({params: baseParams});
					}
		        })
		    });
			// 设置Grid高度和宽度
			Anynote.resizeGridTo("roleListGrid", 0, 56);
			
			// 添加按钮
			$("#roleList-add-button").click(function(){
				
				// 新增用户角色Form
				var addUserFormPanel = new Ext.FormPanel({
					id: 'addUserFormPanel',
			        labelWidth: 60,
			        buttonAlign: 'center',
			        border: false,
			        bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			        url: '',
		            items:[{// 角色名
			                xtype:'textfield',
			                id:'role_name',
			                name: 'role_name',
			                fieldLabel: '角色名',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20
			           },{// 角色描述
			               xtype:'textarea',
			               id:'role_describe',
			               name:'role_describe',
			               fieldLabel:'角色描述',
			               anchor:'98%',
			               allowBlank:false,
			               maxLength: 200
			           }],
			        buttons: [{
	                    text:'提交',
	                    handler: function(){
				        	var form = addUserFormPanel.getForm();
						    if(form.isValid()){
						    	// 发送请求
								Anynote.ajaxRequest({
									baseUrl: '<%=baseUrl %>',
									baseParams: form.getValues(),
									action: '${pageContext.request.contextPath}/registerRoleInfor.action',
									callback: function(jsonResult){
										roleListGrid.getStore().reload();
										addUserWindow.close();
									},
									showWaiting: true,
									failureMsg: '保存失败.'
								});
							}
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                		addUserFormPanel.getForm().reset();
	                    }
	                }]
			    });
				// 用户注册窗口
				var addUserWindow = new Ext.Window({
					title: '添加角色',
					width: 330,
					height: 200,
					modal: true,
					maximizable: false,
					resizable: false,
					iconCls: 'user_add',
					layout:'fit',
					plain: true,
					items: [addUserFormPanel]
				}).show();
			});

			// 启用编辑用户角色
			$("#roleList-update-button").click(function(){
				// 修改
				submit("update");
			});

			// 删除按钮
			$("#roleList-delete-button").click(function(){
				// 删除
				submit("delete");
			});
			
			// 设置权限
			$("#roleList-setting-button").click(function(){
				// 设置权限
				submit("setting");
			});

			// 提交
			function submit(type){
				var msg = "";
				var status = 0;
				if(type=="delete"){
				    status = 1;
					msg = "确定要删除该角色吗？";
				}else if(type=="update"){
				    status = 2;
				}else if(type=="setting"){
				    status = 3;
				}
				var records = roleListGrid.getSelectionModel().getSelections();
				
				if(records.length < 1){
					 Ext.Msg.alert("提示", "请至少选择一条数据.");
				}else{
					   //如果点击的是删除角色
					   if(status == 1){
					      Ext.Msg.confirm("警告", msg, function(btn){
						  if(btn=="yes"){
							var roleIds = "";
							for(var i=0;i<records.length;i++){
								roleIds = roleIds + records[i].get("role_id") + ",";
							}
							// 发送请求
							Anynote.ajaxRequest({
								baseUrl: '<%=baseUrl %>',
								baseParams: {roleIds:roleIds, status:status},
								action: '${pageContext.request.contextPath}/deleteRoleInfor.action',
								callback: function(jsonResult){
									roleListGrid.getStore().reload();
								},
								showWaiting: true,
								failureMsg: '保存失败.'
							});
						   }
					     });
					   //如果点击的是编辑角色
					   }else if(status == 2){
					        var roleIds = "";
						    roleIds = records[0].get("role_id");
					        //只能一条一条的编辑，不能多个一起编辑
					        if(records.length == 1){
						        editRoleWindow = new Ext.Window({
								title: '编辑角色',
								width: 330,
								height: 200,
								modal: true,
								maximizable: false,
								resizable: false,
								layout:'fit',
								iconCls: 'user_edit',
								plain: true,
								autoLoad:{url:'${pageContext.request.contextPath}/page/user/editRole.jsp?roleId='+roleIds,scripts:true,nocache:true},
								listeners: {
									close: function(){

									}
								}
								});
								editRoleWindow.show();
					        }else{
					            Ext.Msg.alert("提示", "只能选择一条数据.");
					        }
					   //如果点击的是权限设置
					   }else if(status == 3){
					        var roleIds = "";
						    roleIds = records[0].get("role_id");
					        //只能一条一条的设置，不能多个一起设置
					        if(records.length == 1){
					            editPerWindow = new Ext.Window({
								title: '设置权限',
								width: 330,
								height: 400,
								autoScroll : true,
								modal: true,
								maximizable: false,
								resizable: false,
								iconCls: 'user_go',
								layout:'fit',
								plain: true,
								autoLoad:{url:'${pageContext.request.contextPath}/page/user/permissionSet.jsp?roleId='+roleIds,scripts:true,nocache:true},
								listeners: {
									close: function(){
                                        if(Ext.getCmp('ptree') != null){Ext.getCmp('ptree').destroy()};//把它destroy就可以了
									}
								}
								});
								editPerWindow.show();
					        }else{
					            Ext.Msg.alert("提示", "只能选择一条数据.");
					        }
					   }
					}
			}
});

	</script>
</head>
<body>
<div id="roleListDiv">
	<div id="roleListToolBarDiv"></div>
	<div id="roleListGridDiv" style="width:100%;height:100%;"></div>
</div>
</body>
</html>
